#include "Common.h"
#if defined (_DEBUG) && defined (DETECT_MEM_LEAKS)
	#define _CRTDBG_MAP_ALLOC
	#include <stdlib.h>
	#include <crtdbg.h>
#endif
#include "HndToRawFiles.h"
#include <windows.h>
#include <memory>
#include <string>
#include <vector>
#include <time.h>

#if defined (_DEBUG) //|| defined (__cplusplus)
	#pragma comment (lib, "C:\\AAAfiles\\Anthony\\ImageView\\Debug\\ImageViewd.lib")
#else
	#pragma comment (lib, "C:\\AAAfiles\\Anthony\\ImageView\\Release\\ImageView.lib")
#endif
//! Check the arguments to make sure they are correct
//! \return False if the provided arguments are invalid, true
//! if they are valid
bool checkArgs(int argc, char* argv[])
{
	if (argc != 9 && argc != 10 && argc != 11)
	{
		printf("About: This program will convert a batch of hnd files to 32 bit grayscale raw files.\n\n"
               "Usage: hnd_to_raw hnd_dir hnd_prefix hnd_postfix raw_dir raw_prefix raw_postfix start_index end_index\n"
			   "hnd_dir         - The directory of the hnd files\n"
			   "hnd_prefix      - The prefix of the hnd files\n"
			   "hnd_postfix     - The postfix of the hnd files\n"
			   "raw_dir         - The directory where the raw files will be created.\n"
			   "raw_prefix      - The prefix of the newly created raw files\n"
			   "raw_postfix     - The postfix of the raw files\n"
			   "start/end index - If the hnd_prefix is \"hnd_\", and the hnd_postfix is \"\", and the start_index "
			   "is \"0\", and the end_index is \"100\", all the files between hnd_0.hnd to hnd_100.hnd"
			   " will be converted.\n"
			   "[OPTIONAL] /f(num) This specifies the input numbered filesnames have fixed lengths (with"
			   "trailing zeros. Example: /f3 means that the files will have names like file_001, "
			   "file_002, file_003, etc.\n"
				"[OPTIONAL] /f(num) This specifies the output numbered filesnames have fixed lengths (with"
			   "trailing zeros. Example: /f3 means that the files will have names like file_001, "
			   "file_002, file_003, etc.\n");
		fflush(stdout);

		system("pause");
		return false;
	}

	return true;
}


int main(int argc, char* argv[]) 
{
#if defined (_DEBUG) && defined (DETECT_MEM_LEAKS)
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	//_CrtSetBreakAlloc(149);
#endif
	if (!checkArgs(argc, argv))
		return 0;

	clock_t start, end;

	start = clock();

	char* hndDir, * hndPrefix, * hndPostfix, * rawDir, * rawPrefix, * rawPostfix;
	unsigned int startIndex, endIndex;

//#define START_INDEX 0
//#define END_INDEX 200

#ifndef START_INDEX
	startIndex = atoi(argv[7]);
#else
	startIndex = START_INDEX;
#endif

#ifndef END_INDEX
	endIndex = atoi(argv[8]);
#else
	endIndex = END_INDEX;
#endif

	hndDir     = argv[1];
	hndPrefix  = argv[2];
	hndPostfix = argv[3];
	rawDir     = argv[4];
	rawPrefix  = argv[5];
	rawPostfix = argv[6];

	int inFixedWidth, outFixedWidth;
	if (argc >= 10)
		inFixedWidth = atoi(argv[9] + 2);
	else
		inFixedWidth = -1;

	if (argc == 11)
		outFixedWidth = atoi(argv[10] + 2);
	else
		outFixedWidth = -1;


	HndToRawFiles(hndDir, hndPrefix, hndPostfix, rawDir,
		          rawPrefix, rawPostfix, startIndex, endIndex,
				  inFixedWidth, outFixedWidth);

	end = clock();
	printf("Took %f s\n", ((float)(end - start)) / CLOCKS_PER_SEC);
	fflush(stdout);
	return 0;
}
